<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="帖子标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入帖子标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发帖人" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入发帖人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="postList" border>
      <el-table-column label="ID" prop="id" width="80" align="center" />
      <el-table-column label="标题" prop="title" min-width="150" />
      <el-table-column label="内容" prop="content" min-width="200">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <div style="max-width: 300px">{{ scope.row.content }}</div>
            <div slot="reference">{{ scope.row.content | ellipsis }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="发帖人" prop="nickName" width="120" align="center" />
      <el-table-column label="发布时间" prop="createTime" width="160" align="center" />
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            :loading="deleteLoading[scope.row.id]"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listPost, delPost } from '@/api/post'
import Pagination from '@/components/Pagination'

export default {
  name: 'PostTable',
  components: { Pagination },
  filters: {
    ellipsis(value) {
      if (!value) return ''
      if (value.length > 20) {
        return value.slice(0, 20) + '...'
      }
      return value
    }
  },
  data() {
    return {
      // 加载状态
      loading: true,
      // 删除按钮加载状态
      deleteLoading: {},
      // 总条数
      total: 0,
      // 帖子表格数据
      postList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        author: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询帖子列表 */
    getList() {
      this.loading = true
      listPost(this.queryParams).then(response => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('确认删除"' + row.title + '"的帖子吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$set(this.deleteLoading, row.id, true)
        delPost(row.id).then(response => {
          this.$modal.msgSuccess('删除成功')
          this.getList()
          this.$delete(this.deleteLoading, row.id)
        })
      }).catch(() => {
        this.$delete(this.deleteLoading, row.id)
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
